var app = getApp();
var net = require("../../utils/net.js");
var utils = require("../../utils/util.js");
var posting = false;
var interval = null //倒计时函数
var dialog = null;
Page({
  data: {
    date: '请选择日期',
    fun_id: 2,
    time: '获取验证码', //倒计时 
    currentTime: 61,
    mobile: "",
    new_psw: "",
    code: "",
    pswnewconfirm: ""
  },
  input: function (e) {
    var value = e.detail.value + "";
    var key = e.currentTarget.id + "";
    console.log(key);

    this.setData({
      [key]: value,
    });
    if (this.data.mobile.length != "") {
      this.showtip(5, "未输入手机号");
      return;
    }
    else if (this.data.mobile.length < 11) {
      this.showtip(5, "请输入正确手机号");
      return;
    }
    if (this.data.code > 4) {
      this.showtip(5, "验证码错误");
      return;
    }
    if (old_psw == new_psw) {
      this.showtip(5, "密码必须一致");
      return false;
    }
  },
    getCode: function (options) {

      var that = this;
      if (this.data.mobile.length == "") {
        this.showtip(1, "未输入手机号");
        return;
      }
     else if (this.data.mobile.length < 11) {
        this.showtip(1, "请输入正确手机号");
        return;
      } else{
        net.request({
          //上线接口地址要是https测试可以使用http接口方式
          url: '/GetCode',
          data: {
            mobile: that.data.mobile,
          },
          method: 'POST',
          success: function (res) {
            if (res.data.flag) {
              that.data.currentTime
              var currentTime = that.data.currentTime
              interval = setInterval(function () {
                currentTime--;
                that.setData({
                  time: currentTime + '秒'
                })
                if (currentTime <= 0) {
                  clearInterval(interval)
                  that.setData({
                    time: '重新发送',
                    currentTime: 61,
                    disabled: false
                  })
                }
              }, 1000)
              that.setData({
                disabled: true
              })
            } else {
              dialog.showDialog({
                msg: res.data.msg
              });
            }
          }, fail(res) {
            console.log(res)
          }
        })
      }
     
    
    },
    getVerificationCode() {
      this.getCode();
      var that = this
      that.setData({
      
      })
    },
    inputtip(val, key) {
      this.setData({
        [key]: val
      })
    },
    showtip(lv, msg) {
      this.setData({
        tipno: lv,
        tipmsg: msg
      })
    },
    Confirm: function () {
      this.setData({
        hidden: true
      })
    },
    reset: function () {
      var that = this;
      var old_psw = this.data.pswnewconfirm;
      var new_psw = this.data.new_psw;
      if (this.data.mobile.length < 11) {
        this.showtip(1, "请输入正确的手机号码");
        return;
      }
      if (this.data.code < 4) {
        this.showtip(1, "验证码错误");
        return;
      }
      if (old_psw!= new_psw) {
        this.showtip(1, "密码必须一致");
        return false;
      } else if (old_psw == new_psw) {
      this.yanzheng();
      }
    },
  yanzheng:function(){
    var that = this;
    net.request({
      //上线接口地址要是https测试可以使用http接口方式
      url: '/FindPsw',
      data: {
        mobile: that.data.mobile,
        pswnewconfirm: that.data.pswnewconfirm,
        new_psw: that.data.new_psw,
        code: that.data.code
      },
      method: 'POST',
      success: function (res) {
        console.log(res)
        if (res.data.flag) {
          wx.redirectTo({
            url: '../index/index',
            success: function (res) { },
            fail: function (res) { },
            complete: function (res) { },
          })
        } else {
          that.showtip(3, res.data.msg);

        }
      }, fail(res) {
        console.log(res)
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
   onLoad: function () {
   
    },
   
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    dialog = this.selectComponent("#dialog");
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})